package org.x.util;

public class PNGCompress {
    static
    {
        System.loadLibrary("pngcompress");
    }
    
    static final int DSTBUFF_ERROR = -2; 
    static final int SRCBUFF_ERROR = -1;
    static final int SUCCESS = 0;                                                                                                                                                                                         
    static final int MISSING_ARGUMENT = 1;
    static final int READ_ERROR = 2;
    static final int INVALID_ARGUMENT = 4;
    static final int NOT_OVERWRITING_ERROR = 15; 
    static final int CANT_WRITE_ERROR = 16;
    static final int OUT_OF_MEMORY_ERROR = 17; 
    static final int WRONG_ARCHITECTURE = 18; // Missing SSE
    static final int PNG_OUT_OF_MEMORY_ERROR = 24; 
    static final int LIBPNG_FATAL_ERROR = 25;
    static final int WRONG_INPUT_COLOR_TYPE = 26; 
    static final int LIBPNG_INIT_ERROR = 35;
    static final int TOO_LARGE_FILE = 98;
    static final int TOO_LOW_QUALITY = 99; 
    
    byte[] dstBuf = null;
    int dstSize = 0;
    int retValue = SUCCESS;
    
    public byte[] getDstBuf() {
    	return this.dstBuf;
    }
    
    public int getDstSize() {
    	return this.dstSize;
    }
    
    public int getRetValue() {
    	return this.retValue;
    }
    
    /**
     * @param input_buf  : Input buffer.
     * @param output_buf : Output buffer, output parameter, this buffer while malloc in function, if not NULL when return, you need free it.
     * @param out_length : Output buffer length, output parameter.
     * @param quality    : Quality should be in format min-max where min and max are numbers in range 0-100.
     * @param speed      : Speed should be between 1 (slow) and 11 (fast) or 0 (for not set speed).
     * @param ordered    : If true, floyd while set to 0.
     * @param floyd      : Must be in 0..1 range, default set to 1.0.
     * @param verbose    : Print debug info if true.
     * @return png byte array.
     * @return retValue is return form c code, 0 success, < 0 memory error, > 0 compress error.
     */
    public byte[] compress(byte[] srcBuf, String quality, int speed, boolean ordered, float floyd, boolean verbose) {
    	if (srcBuf == null)
    		return null;
    	
    	byte[] buff = nativeCompress(srcBuf, quality, speed, ordered, floyd, verbose);
    	
    	return buff;
    }
    
    private native byte[] nativeCompress(byte[] srcBuf, String quality, int speed, boolean ordered, float floyd, boolean verbose);
}
